package com.bobo.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bobo.rbac.domain.User;
import com.bobo.rbac.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: BOBO
 * @time: 2022/7/24 18:26
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  /*      LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);*/
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("enabled","1");
        User user = userMapper.selectOne(queryWrapper);//获取

    /*
        List<SimpleGrantedAuthority> objects = new ArrayList<>();
        for (String s : roleList) {
            objects.add(new SimpleGrantedAuthority(s));
        }*/
        //根据用户查询角色
        List<String> roleList = userMapper.getRole(user.getId());
        List<SimpleGrantedAuthority> collect = roleList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return new org.springframework.security.core.userdetails.User(username,user.getPassword(),collect);
    }
}
